package com.mai.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mai.entity.Product;
import com.mai.service.ProductService;
import com.mai.util.UploadUtil;
import com.zwj.entity.ZOrderMaster;
import com.zwj.service.ZOrderMasterService;

@Controller
public class ProductController {
	
	@Autowired
	private ProductService ps;
	
	@Autowired
	private ZOrderMasterService zOrderMasterService;
	/**
	 * 查询所有商品
	 */
	@RequestMapping("selectAllProduct")
	public String selectAllProduct(@RequestParam(value="pageNum", required = true,defaultValue="1") int pageNum,@RequestParam(value="pageSize", required = true,defaultValue="4") int pageSize, Model model) {
		PageHelper.startPage(pageNum, pageSize);
		List<Product> products = ps.selectAllProduct();
		PageInfo<Product> page1 = new PageInfo<Product>(products);
		model.addAttribute("products", products);
		model.addAttribute("page1", page1);
		
		PageHelper.startPage(pageNum, pageSize);
		List<ZOrderMaster> lom = this.zOrderMasterService.selectAll();
		PageInfo<ZOrderMaster> page=new PageInfo<ZOrderMaster>(lom);
		model.addAttribute("lom", lom);
		model.addAttribute("page", page);
		return "houtai";		
	}
	
	/**
	 * 模糊查询
	 */
	@RequestMapping("Mohu")
	public String MuHu(@RequestParam(value="pageNum", required = true,defaultValue="1") int pageNum,@RequestParam(value="pageSize", required = true,defaultValue="4") int pageSize,String p_c, Model model) {
		PageHelper.startPage(pageNum, pageSize);
		List<Product> products = ps.Mohu(p_c);
		PageInfo<Product> page = new PageInfo<Product>(products);
		
		model.addAttribute("products", products);
		model.addAttribute("page1", page);
		return "houtai";	
	}
	
	/**
	 * 根据id查询商品
	 */
	@ResponseBody
	@RequestMapping("findProductById")
	public Product findProductById(int uproduct_id) {
		Product p = ps.selectProductById(uproduct_id);
		return p;
	}
	
	/**
	 * 添加商品
	 */
	@RequestMapping("addProduct")
	public String addProduct(MultipartFile file, String product_name, double product_price, String product_category, String product_description, HttpServletRequest request) {
		String product_icon = UploadUtil.UploadFile(file, request);
		Product p = new Product(product_name, product_price, product_description, product_icon, product_category);
		boolean addProduct = ps.addProduct(p);
		return "forward:selectAllProduct";
		
	}
	
	/**
	 * 修改商品
	 */
	@RequestMapping("updateProduct")
	public String updateProduct(MultipartFile ufile,int uproduct_id, String uproduct_name, double uproduct_price, String uproduct_category, String uproduct_description, HttpServletRequest request) {
		if(ufile.getSize() != 0) {		
			String product_icon = UploadUtil.UploadFile(ufile, request);
			Product p = new Product(uproduct_id, uproduct_name, uproduct_price, uproduct_description, product_icon, uproduct_category);
			ps.updateProduct(p);
			return "forward:selectAllProduct";
		}else {
			
			Product p = new Product(uproduct_id, uproduct_name, uproduct_price, uproduct_description, uproduct_category);
			ps.updateProduct(p);
			return "forward:selectAllProduct";
		}
	}
	
	/**
	 * 删除商品
	 */
	@ResponseBody
	@RequestMapping("deleteProduct")
	public String deleteProduct(int dproduct_id) {	
		if(ps.deleteProductById(dproduct_id)) {
			return "0";
		}else {
			return "1";
		}	
	}
}
